<?php

    // +----------------------------------------------------------------------
    // | 投诉管理
    // +----------------------------------------------------------------------
    // | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
    // +----------------------------------------------------------------------
    // | Author: 微尘 <yicmf@qq.com>
    // +----------------------------------------------------------------------

    namespace app\controller\ucenter\admin;

    use yicmf\builder\View;
    use app\model\ucenter\Poster as ComplaintModel;
    use app\event\ucenter\Complaint as ComplaintEvent;
    use yicmf\builder\Edit;
    use yicmf\builder\Table;
    use app\controller\admin\Common;

    class Complaint extends Common
    {
        protected function initialize()
        {
            parent::initialize();
        }

        public function _filter()
        {
            $map = [];
            if (!$this->request->param('status')) {
                $map[] = ['status', '>', -1];
            }
            return $map;
        }

        /**
         * 订单列表
         * @return mixed|string
         * @throws \Exception
         * @author  : 微尘 <yicmf@qq.com>
         * @datetime: 2019/4/27 21:09
         */
        public function index()
        {
            // 读取规则列表
            $statusOptions = [
                [
                    'id' => 0,
                    'value' => '待处理',
                ],
                [
                    'id' => 1,
                    'value' => '处理中',
                ],
                [
                    'id' => 2,
                    'value' => '介入',
                ],
                [
                    'id' => 5,
                    'value' => '已完结',
                ]
            ];
			$map = $this->_filter();
			$map[] = ['order_id', '>', 0];
            // 显示页面
            $builder = new Table();
            $builder->model(ComplaintModel::class)
                ->where($map)
                ->order('id DESC')->setSearchPostUrl('', '')
                ->searchText('complaint_sn', '投诉编号')
                ->searchText('order_sn', '订单编号')
                ->searchSelect('status', '状态：', $statusOptions)
                ->keyId()
				->keyHidden('order_id')
				->keyText('complaint_sn', '投诉编号')
				->keyText('type', '投诉类型')
//				->keyDialog('order_sn', '投诉订单','store/admin/Order/view?id={{d.order_id}}')//
				->keyText('content', '投诉内容')
                ->keyUser('user_id', '投诉客服')
                ->keyCreateTime()
                ->keyIp('ip', '投诉者IP')
                ->keyMap('status', '状态', [0 => '待审核', 1 => '处理中', 2 => '平台介入', 5 => '已完结'])
				->actionView('view?id={$id}', '查看',[0,1,2,5])
                ->actionUpdate('update?id={$id}', '处理', [1, 2])
                ->actionDelete('del?id={$id}', '删除', [5]);
            return $builder->fetch();
        }

		/**
		 * 订单投诉
		 * @return mixed|string
		 * @throws \Exception
		 * @author  : 微尘 <yicmf@qq.com>
		 * @datetime: 2019/4/27 21:09
		 */
		public function good()
		{
			// 读取规则列表
			// 显示页面
			$statusOptions = [
				[
					'id' => 0,
					'value' => '待处理',
				],
				[
					'id' => 1,
					'value' => '处理中',
				],
				[
					'id' => 2,
					'value' => '介入',
				],
				[
					'id' => 5,
					'value' => '已完结',
				]
			];
			$map = $this->_filter();
			$map[] = ['order_id', '=',null];
			// 显示页面
			$builder = new Table();
			$builder->model(ComplaintModel::class)
				->where($map)
				->order('id DESC')
				->setSearchPostUrl('', '')
				->searchSelect('status', '状态：', $statusOptions)
				->keyId()
				->keyHidden('good_id')
				->keyText('complaint_sn', '投诉编号')
				->keyText('type', '投诉类型')
				->keyDialog('title', '投诉商品','store/admin/Good/view?id={{d.good_id}}')//
				->keyText('content', '投诉内容')
				->keyCreateTime()
				->keyIp('ip', '投诉者IP')
				->keyStatus([0 => '待处理', 1 => '处理中', 2 => '平台介入', 5 => '已完结'])
				->actionView('view?id={$id}', '查看',[0,1,2,5])
				->actionUpdate('update?id={$id}', '处理', [0, 1]);
			return $builder->fetch();
		}

        public function del()
        {
            $order = $this->model->find($this->request->get('id'));
            if (1 == $order['delete_status']) {
                $order['delete_status'] = 3;
            } else {
                $order['delete_status'] = 2;
            }
            if ($order->save()) {
                $this->_returnSuccess( '删除成功');
            } else {
                $this->_returnError( '失败，失败信息：');
            }
        }

        /**
         * 浏览投诉
         * @return mixed|string
         * @throws \Exception
         * @author  : 微尘 <yicmf@qq.com>
         * @datetime: 2019/5/8 16:13
         */
        public function view()
        {
			$follow = ComplaintModel::where('id',$this->request->get('id'))->find();
			if (!$follow) {
				$this->_returnError( '信息不存在');
			}
			$view = new View();
			$view->title('投诉详情')
				->keyLabel('qq', '联系QQ')
				->keyLabel('mobile', '联系手机')
				->keyLabel('order.order_sn', '关联订单')
				->keyLabel('store.nickname', '关联商家')
				->keyLabel('store.mobile', '商家手机')
				->keyClosure('处理日志', function ($data) {
					$html = '';
					$logs = $data->logs()->order('create_time DESC')->select();
					foreach ($logs as $index => $log) {
						$html .= '【' . $log['create_time'] . '】' . '【' . $log['ip'] . '】' . $log['message'] . '<br>';
					}
					return $html;
				})
				->keyLabel('ip', '下单ip')
				->data($follow);
			return $view->fetch();
        }

        /**
         * 编辑订单价格备注
         * @return string|void
         * @throws \Exception
         * @author  : 微尘 <yicmf@qq.com>
         * @datetime: 2019/3/28 13:02
         */
        public function update()
        {
            if ($this->request->isPost()) {
                $event = new ComplaintEvent();
                $result = $event->reply($this->request->post(), $this->user);
                if (0 == $result['code']) {
                    $this->_returnSuccess( $result['message'], true);
                } else {
                    $this->_returnError( $result['message']);
                }
            } else {
                // 判断是否为编辑模式
                $isEdit = $this->request->has('id') ? true : false;
                $builder = new Edit();
                if ($isEdit) {
                    $data = ComplaintModel::where('id', $this->request->param('id'))->find();
                } else {
                    $this->_returnError( '投诉单不存在');
                }
                $builder->title('处理投诉')
                    ->keyLabel('qq', 'QQ')
                    ->keyLabel('mobile', '手机')
                    ->keyLabel('email', '邮件')
                    ->keyLabel('connect', '联系方式')
                    ->keyLabel('type', '投诉类型')
                    ->keyLabel('order.order_sn', '关联订单')
				->keyClosure('处理日志', function ($data) {
					$html = '';
					$logs = $data->logs()->order('create_time DESC')->select();
					foreach ($logs as $index => $log) {
						$html .= '【' . $log['create_time'] . '】' . '【' . $log['ip'] . '】' . $log['message'] . '<br>';
					}
					return $html;
				});
                //                    ->keyImageModels('image', '上传凭证', '最多5张凭证', 2)
                $builder->keyRadio('result', '处理结果', ['投诉失败', '投诉成立'], '判断投诉成立后订单会退款处理')
                    ->keyTextArea('message', '处理内容')
                    ->data($data)
                    ->buttonSubmit('', '提交')
                    ->buttonClose();
                return $builder->fetch();
            }
        }

    }
